<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="用户名称" prop="userEntity.username">
        <el-input v-model="dataForm.userEntity.username" placeholder="用户名称"></el-input>
      </el-form-item>
      <el-form-item label="员工编号" prop="empNum">
        <el-input v-model="dataForm.empNum" placeholder="员工编号" :readonly="dataForm.id ? true : false"></el-input>
      </el-form-item>
      <el-form-item label="员工姓名" prop="empName">
        <el-input v-model="dataForm.empName" placeholder="员工姓名"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio v-model="dataForm.sex" label="男">男</el-radio>
        <el-radio v-model="dataForm.sex" label="女">女</el-radio>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="联系传真" prop="fax">
        <el-input v-model="dataForm.fax" placeholder="联系传真"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="电子邮箱"></el-input>
      </el-form-item>
      <el-form-item label="邮编" prop="zip">
        <el-input v-model="dataForm.zip" placeholder="邮编"></el-input>
      </el-form-item>
      <el-form-item label="联系地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="联系地址"></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: '',
          empNum: '',
          empName: '',
          sex:'',
          phone:'',
          fax:'',
          email:'',
          zip:'',
          address:'',
          userEntity:{
            username:''
          },
        },
        dataRule: {
          empNum: [
            { required: true, message: '请输入员工编号', trigger: 'blur' }
          ],
          empName: [
            { required: true, message: '请输入员工姓名', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '请选择性别', trigger: 'blur' }
          ],
          phone: [
            { pattern:'^[0-9]*$', required: true, message: '请输入规范的手机号码', trigger: 'blur' }
          ],
          fax: [
            {pattern:'^[0-9]*$', required: true, message: '请输入规范的传真', trigger: 'blur' }
          ],
          email: [
            { type:"email", required: true, message: '请输入规范的电子邮箱', trigger: 'blur' }
          ],
          zip: [
            {pattern:'^[0-9]*$', required: true, message: '请输入规范的邮编', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '请输入地址', trigger: 'blur' }
          ],
          userEntity:{
            username: [
              {required: true, message: '请输入用户名称7', trigger: 'blur'},
            ]
          }
        },
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.dataForm.id = id
        if (id){
          this.$http({
            url: this.$http.adornUrl(`/purchase/employee/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then((data) => {
            if (data && data.code === 200) {
              this.dataForm = data.employee
            }
          })
        }else {
          // 拿到表单原始数据,清空表单
          this.dataForm = this.$options.data.call(this).dataForm;
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/employee/${!this.dataForm.id ? 'saveEmpAndUser' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'empNum': this.dataForm.empNum,
                'empName': this.dataForm.empName,
                'sex': this.dataForm.sex,
                'phone': this.dataForm.phone,
                'fax': this.dataForm.fax,
                'email': this.dataForm.email,
                'zip': this.dataForm.zip,
                'address': this.dataForm.address,
                'userEntity':this.dataForm.userEntity
              })
            }).then((data) => {
              if (data && data.code ===200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error("操作失败")
              }
            })
          }
        })
      }
    }
  }
</script>
